/**
 * ...
 * @author Rob Dravenstott
 */

class GraphicUtils 
{
	
	public static function createRect(mc:MovieClip, name:String, x:Number, y:Number, width:Number, height:Number, fill:Number, transparency:Number, lineWidth:Number):Void
	{
		mc.createEmptyMovieClip(name, mc.getNextHighestDepth());
		mc[name].beginFill(fill, transparency);
		if ((lineWidth != undefined)
		    && (lineWidth > 0))
			mc[name].lineStyle(lineWidth, 0xFFFFFF, 100);
		mc[name].moveTo(x, y);
		mc[name].lineTo(x+width, y);
		mc[name].lineTo(x+width, y+height);
		mc[name].lineTo(x, y+height);
		mc[name].lineTo(x, y);
		mc[name].endFill();
	}
	
	public static function createCircleText(mc:MovieClip, name:String, x:Number, y:Number, r:Number, fill:Number, transparency:Number, pressFunc:Function):Void
	{
		mc.createEmptyMovieClip(name, mc.getNextHighestDepth());
		mc[name].beginFill(fill, transparency);
		mc[name].moveTo(x+r, y);
		mc[name].curveTo(r+x, Math.tan(Math.PI/8)*r+y, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
		mc[name].curveTo(Math.tan(Math.PI/8)*r+x, r+y, x, r+y);
		mc[name].curveTo(-Math.tan(Math.PI/8)*r+x, r+y, -Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
		mc[name].curveTo(-r+x, Math.tan(Math.PI/8)*r+y, -r+x, y);
		mc[name].curveTo(-r+x, -Math.tan(Math.PI/8)*r+y, -Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
		mc[name].curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, x, -r+y);
		mc[name].curveTo(Math.tan(Math.PI/8)*r+x, -r+y, Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y);
		mc[name].curveTo(r+x, -Math.tan(Math.PI/8)*r+y, r+x, y);
		mc[name].endFill();
		
		mc[name].createTextField("t", mc.getNextHighestDepth(), x-r, y-r, r*2, r*2);
		
		var tf:TextFormat = new TextFormat();
		tf.size = 32;
		tf.align = "center";
		tf.color = 0xFFFFFF; //white text
		tf.bold = true;
		tf.font = "_sans"; //also _serif and _typewriter available for chumby

		mc[name].t.setNewTextFormat(tf);
		mc[name].t.textColor = 0xFFFFFF;
		mc[name].t.text = name;											
		mc[name].t.wordWrap = false;
		delete tf;
		
		// Create an invisible rectangle to use for pushing
		GraphicUtils.createRect(mc[name], "btn", x - r, y - r, r * 2, r * 2, 0x000000, 0);
		mc[name].btn.onPress = pressFunc;
	}
	
	public static function createTextArrows(mc:MovieClip, tf:TextFormat, text_bottom_mc_name:String, text_field_mc_name:String, name_up:String, name_down:String, x:Number, y:Number, text_width:Number)
	{
		mc.createTextField(text_bottom_mc_name, mc.getNextHighestDepth(), x, y, text_width, 30);
		tf.size = 20;
		tf.align = "right";
		mc[text_bottom_mc_name].setNewTextFormat(tf);
		
		mc.createTextField(text_field_mc_name, mc.getNextHighestDepth(), x, y+30, text_width, 28);
		tf.size = 20;
		mc[text_field_mc_name].setNewTextFormat(tf);
		mc[text_field_mc_name].background = true;
		mc[text_field_mc_name].backgroundColor = 0x999999

		GraphicUtils.createUpDownArrows (mc, name_up, name_down, x+text_width+10, y+40, 0xFFFFFF, 100);
	}
	
	public static function createUpDownArrows (mc:MovieClip, name_up:String, name_down:String, x:Number, y:Number, fill:Number, transparency:Number):Void
	{
		var vertical_spacer:Number = 9;
		var name_up_drawing = name_up + "_drawing";
		var name_down_drawing = name_down + "_drawing";
		var width:Number = 44;
		var height:Number = 36;
		
		mc.createEmptyMovieClip(name_up_drawing, mc.getNextHighestDepth());
		mc[name_up_drawing].beginFill(fill, transparency);
		mc[name_up_drawing].moveTo(x, y);
		mc[name_up_drawing].lineTo(x+width, y);
		mc[name_up_drawing].lineTo(x+width/2, y-height);
		mc[name_up_drawing].lineTo(x, y);
		mc[name_up_drawing].endFill();

		mc.createEmptyMovieClip(name_up, mc.getNextHighestDepth());
		mc[name_up].beginFill(0x000000, 0);
		mc[name_up].moveTo(x-10, y-height-10);
		mc[name_up].lineTo(x+width+10, y-height-10);
		mc[name_up].lineTo(x+width+10, y);
		mc[name_up].lineTo(x-10, y);
		mc[name_up].lineTo(x-10, y-height-10);
		mc[name_up].endFill();
		
		mc.createEmptyMovieClip(name_down+"_drawing", mc.getNextHighestDepth());
		mc[name_down_drawing].beginFill(fill, transparency);
		mc[name_down_drawing].moveTo(x, y+vertical_spacer);
		mc[name_down_drawing].lineTo(x+width, y+vertical_spacer);
		mc[name_down_drawing].lineTo(x+width/2, y+vertical_spacer+height);
		mc[name_down_drawing].lineTo(x, y+vertical_spacer);
		mc[name_down_drawing].endFill();
		
		mc.createEmptyMovieClip(name_down, mc.getNextHighestDepth());
		mc[name_down].beginFill(0x000000, 0);
		mc[name_down].moveTo(x-10, y+vertical_spacer);
		mc[name_down].lineTo(x+width+10, y+vertical_spacer);
		mc[name_down].lineTo(x+width+10, y+vertical_spacer+height+10);
		mc[name_down].lineTo(x-10, y+vertical_spacer+height+10);
		mc[name_down].lineTo(x-10, y+vertical_spacer);
		mc[name_down].endFill();
	}
	
}